package servlet.Mathe;

import bean.Course;
import bean.Mathe;
import bean.Sc;
import dao.impl.CourseDaoImpl;
import dao.impl.ScDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/sc/updateServlet")
public class updateSc extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");//请求编码
        resp.setContentType("text/html;charset=utf-8");//响应编码
        String sno = req.getParameter("sno");
        ScDaoImpl scdao = new ScDaoImpl();
        Mathe mathe = scdao.GetScBySno(sno);
        CourseDaoImpl dao = new CourseDaoImpl();
        List<Course> course = dao.getCourseAll();
        HttpSession session = req.getSession();
        session.setAttribute("mathes", mathe);
        session.setAttribute("courseList", course);
        resp.sendRedirect("/sc/updateSc.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");//请求编码
        resp.setContentType("text/html;charset=utf-8");//响应编码
        String sno = req.getParameter("sno");
        String cno = req.getParameter("cno");
        String grade = req.getParameter("grade");
        ScDaoImpl scdao = new ScDaoImpl();
        Sc sc = new Sc();
        sc.setSno(sno);
        sc.setCno(cno);
        sc.setGrade(Double.valueOf(grade));
        HttpSession session = req.getSession();
        int row = scdao.UpdateSc(sc);
        if (row > 0) {
            req.getRequestDispatcher("/MatheAllServlet").forward(req,resp);
        }else{
            session.setAttribute("msg","内部错误，稍后重试！"); //存储到缓存
            resp.sendRedirect("/err/error.jsp");
        }
    }
}
